package org.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.config.ExcelConfig;
import org.example.mapper.CensusMapper;
import org.example.pojo.Census;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class App 
{
    //此处使用绝对地址 这个是Excel的表格班级 这两个文件都必须要 第一次初始化运行获取名单，第二次运行即可查找未交名单
    private static String ExcelPath="C:\\Users\\admin\\Downloads\\****.xlsx";
    //这个是提交作业的需比对的文件夹
    private static String filePath="C:\\Users\\admin\\Desktop\\****";

    private ExcelConfig excelReader=new ExcelConfig();
    private SqlSessionFactory sqlSessionFactory;
    private static List<String> confirmedStudents=new ArrayList<>();
    private void initMybatis() throws IOException {
        // 1. 加载 MyBatis 配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 2. 创建 SqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 3. 获取 SqlSession 并执行查询
        loadConfirmedStudents();
    }
    private void loadConfirmedStudents() {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            CensusMapper mapper = session.getMapper(CensusMapper.class);
            confirmedStudents = mapper.getAllStuName();
            if(!confirmedStudents.isEmpty())
            {
                mapper.updateInit();
            }
            else
            {
                //导入学生信息 识别标题为姓名的列，跳过第一行 从第二行开始
                //importPersonByHeaderExcel(ExcelPath,"论文题目",1);
                //从第B列开始，第1行
                /**
                 * 用于设置参数 这里采用的是列行定位 这里的1，2   对应的是B3位置开始所有向下的名字
                 */
                importPersonByIndexExcel(ExcelPath,1,2);
            }
            session.commit();
        }
    }
    private void checkFolder(String folderPath)
    {
        File file=new File(folderPath);
        System.out.println("文件为:"+file);
        if(!file.exists()||!file.isDirectory())
        {
            System.out.println("文件夹不存在或不是有效的目录");
            return;
        }
        File[] files=file.listFiles();
        try(SqlSession sqlSession=sqlSessionFactory.openSession())
        {
            CensusMapper mapper = sqlSession.getMapper(CensusMapper.class);
            for (File f : files) {
                String fileName = f.getName();
                String studentName = extractStudentName(fileName);
                //查到学生进行更新
                if (studentName != null) {
                    Census stu = mapper.getStuByName(studentName);
                    Census census=new Census(studentName,stu.getNumber(),true, stu.getSubmitCount()+1);
                    mapper.update(census);
                }
                sqlSession.commit();
            }
            List<String> notCommitName = mapper.getNotCommitName();
            System.out.println("未提交的同学共有"+notCommitName.size());
            for (String name:notCommitName)
            {
                System.out.println("未提交的同学为========》"+name);
            }

        }
        System.out.println("当前已提交同学======》"+files.length+"个");

    }
    //Todo 标题检索还未完善
    private void importPersonByHeaderExcel(String excelPath,String headerName,int headerRow)
    {
        ExcelConfig.ReadConfig config= ExcelConfig.ReadConfig.byHeader(headerName,headerRow);
        importPerson(excelPath,config);
    }
    private void importPersonByIndexExcel(String excelPath, int columnIndex, int dataStartRow)
    {
        ExcelConfig.ReadConfig config =
                ExcelConfig.ReadConfig.byIndex(columnIndex, dataStartRow);
        importPerson(excelPath, config);
    }
    private void importPerson(String excelPath,ExcelConfig.ReadConfig config)
    {
        List<String> names = excelReader.readStudentNames(excelPath, config);
        try(SqlSession sqlSession= sqlSessionFactory.openSession())
        {
            CensusMapper censusMapper=sqlSession.getMapper(CensusMapper.class);
            for(String name:names)
            {
                System.out.println("获取到的名字为:"+name);
                Census census=new Census(name,0,false,0);
                censusMapper.insert(census);
            }
            sqlSession.commit();
        }
    }
    private String extractStudentName(String fileName)
    {
        System.out.println("获取到的名字有:"+fileName);
        for(String name:confirmedStudents)
        {
            if(fileName.contains(name))
                return name;
        }
        return "";
    }
    public static void main( String[] args ) throws Exception  {
        App app=new App();
        //加载数据库信息
        app.initMybatis();
        //没有信息，导入Excel报表信息去搜索

        //有信息后进行比对
        if(!confirmedStudents.isEmpty())
            app.checkFolder(filePath);
    }
}
